<?php
class MTranslator {
    /**
     * Translate the message specified by its key.
     * @param String $message : The message to translate
     * @param Array $params (optional) : Parameters of the message
     * @param String $source (optional) : The name of the concerned part of the application (example : a module name). 
     * @param String $category (optional) : The category of the source (example : if it's a module : 'modules')
     * @param String $language (optional) : The language to translate to
     * @return String : The translated message 
     */
    public static function translate($message, $params=NULL, $source=NULL, $category=NULL, $language=NULL) {
        $return = NULL;
        if (Mandrill::getApp() !== NULL) {
            $pathFound = false;
            $request = new MRequest();
            $router = MRouter::getInstance();
            $router->getRoute($request);

            $path = NULL;
            $module = $request->getModule();
            $app = Mandrill::getApp();

            $requestParams = $request->getGet();
            if ($module == $app::ADMIN_MODULE && isset($requestParams[MRouter::MODULE_ADMIN_ROUTE_VAR])) {
                $request->isAdmin(true);
                $router->routeVar = MRouter::MODULE_ADMIN_ROUTE_VAR;
                $router->getRoute($request);
                $router->routeVar = MRouter::DEFAULT_ROUTE_VAR;
            }

            $explodedMessage = explode('.', $message);
            if ($explodedMessage[0] == 'mandrill') {
                if (array_search($explodedMessage[1], self::$coreMessages) !== FALSE) {
                    $source = 'mandrill';
                    $path = MD_LIB . DS . 'messages';
                    $pathFound = true;
                } else {
                    if (isset($explodedMessage[1])) {
                        $testPath = BASE_PATH . DS . $app->modulesFolder . DS . $app::MODULE_PREFIX . $explodedMessage[1] . DS . 'messages';
                        if (file_exists($testPath)) {
                            $source = $app::MODULE_PREFIX . $explodedMessage[1];
                            $path = $testPath;
                            $pathFound = true;
                        }
                    }
                }
            }
            if ($source === NULL && !$pathFound) {
                if (isset($module) && !empty($module)) {
                    $source = $request->getModule();
                    $path = BASE_PATH . DS . $app->modulesFolder . DS . $source . DS . 'messages';
                } else {
                    $source = 'mandrill';
                    $path = MD_LIB . DS . 'messages';
                }
            } else if (!$pathFound) {
                if ($category === NULL && $source != 'mandrill') {
                    throw new MException(self::translate('mandrill.utils.translator.category'));
                } else {
                    $path = BASE_PATH . DS . $category . DS . $source . DS . 'messages';
                }
            }
            if ($language === NULL) {
                $language = $app->defaultLanguage !== NULL ? $app->defaultLanguage : 'en';
            }
            if (file_exists($path . DS . $language . '.php')) {
                $path .= DS . $language . '.php';
            } else {
                $path .= DS . 'en.php';
            }

            $array = require($path);
            $return = $array[$message];
            if ($params !== NULL) {
                if (is_array($params)) {
                    foreach ($params as $key => $value) {
                        $return = str_ireplace('{' . $key . '}', $value, $return);
                    }
                }
            }
            unset($array);
        }
        return $return;
    }

    public static $coreMessages = array(
        'base',
        'db',
        'ext',
        'models',
        'utils',
        'web'
    );
}
?>
